#include "AddLogEntry.h"

void AddLogEntry::prompt() const
{
#pragma region variables
	unsigned int pl;
	std::string model_found;
	std::string yearent_str;
	int yearent;
	std::string yearinq;
	std::string yearselect;
	std::string dateent_str;
	int dateent;
	std::string dateinq;
	std::string dateselect;
	std::string callnument;
	std::string callnumselect;
	int deslast_index;
	std::string deslast_airport_id;
	std::string deslast_airport;
	std::string deslast_airport_suf;
	std::string deslast_str;
	int	dep_ind_new;
	std::string dep_id_new;
	std::string dep_new;
	std::string dep_suf_new;
	int	des_ind_new;
	std::string des_id_new;
	std::string des_new;
	std::string des_suf_new;
	std::string depent;
	std::string depinq;
	std::string depselect;
	std::string desent;
	std::string desselect;
	std::string landingent_str;
	int landingent;
	std::string night_landingent_str;
	int night_landingent;
	std::string apprent_str;
	int apprent;
	std::string timeent_str;
	int timeent;
	std::string nightent_str;
	int nightent;
	std::string ifrent_str;
	int ifrent;
	std::string gadent_str;
	int gadent;
	std::string cfient_str;
	int cfient;
	std::string studentnum_str;
	unsigned int studentnum;
	std::string dualent_str;
	int dualent;
	std::string picent_str;
	int picent;
	std::string sicent_str;
	int sicent;
	std::string sftent_str;
	int sftent;
	std::string commentent;
	std::string verify_log;
	std::string newentry;
	std::string new_reent;
	std::string new_plane_order_str;
	int	plane_ent_index;
	std::string plane_ent_index_str;
	std::string new_model;
	std::string new_type;
	std::string new_rg;
	std::string new_hp;
	std::string new_tw;
	std::string new_plane_inq;
	std::string dep_airport;
	std::string dep_airport_suf;
	std::string dep_new_lat_deg_str;
	double dep_new_lat_deg;
	std::string dep_new_lat_min_str;
	double dep_new_lat_min;
	std::string dep_new_lon_deg_str;
	double dep_new_lon_deg;
	std::string dep_new_lon_min_str;
	double dep_new_lon_min;
	std::string des_airport;
	std::string des_airport_suf;
	std::string id_new;
	std::string suf_new;
	std::string name_new;
	std::string new_airport_inq;
	std::string studentent;
	std::string new_ident;
	std::string airport_select;
	int airport_candidates_num = -1;
	std::vector < int > airport_candidates(50);
	std::string airport_list_select_str;
	int airport_list_select;
	//int airport_name = -1;
	std::string nightinq;
	std::string airport_id_used;
	std::string dep_str;
	std::string future;
	std::string proceed;
	std::string precede_entry;
	std::string new_log_entry;
	std::string new_log_year_str;
	std::string new_log_date_str;
	std::string log_year_str;
	std::string log_date_str;
	int log_counter;
	std::string order_found;
	std::string student_first_ent;
	std::string student_last_ent;
#pragma endregion
#if 0
	std::string entry;

	// Choose logbook entry date
	do
	{
		if (time_fun == "y")
		{
			std::cout << "'t'  " << util::int_to_string_date_format(year_today, date_today) << "  Today" << std::endl;      
			std::cout << "'l'  " << log[log.size() - 1].dateStr() << "  Last logbook entry" << std::endl;
			std::cout << "'d'            Different date" << std::endl << std::endl;
			if (time_fun == "y")
				std::cout << "Choose logbook entry date [t]: ";
			if (time_fun == "n")
				std::cout << "Choose logbook entry date [l]: ";
		}

		do
		{
			std::cin >> entry;
			dateinq = entry;
			dateinq = util::all_lower(dateinq);
			if (dateinq == "" && time_fun == "y")
				dateinq = "t";
			if (dateinq == "" && time_fun == "n")
				dateinq = "l";
			if (dateinq != "t" && dateinq != "l" && dateinq != "d" && time_fun == "y")
				std::cout << "        Enter 't', 'l' or 'd': ";
			if (dateinq != "t" && dateinq != "l" && dateinq != "d" && time_fun == "n")
				std::cout << "             Enter 'l' or 'd': ";
		} while(dateinq != "t" && dateinq != "l" && dateinq != "d");

		if (dateinq == "t" && time_fun == "n")
			dateinq = "l";

		if (dateinq == "t")
		{
			yearent_str = year_today_str;
			yearent = year_today;
			dateent_str = date_today_str;
			dateent = date_today;
		}

		if (dateinq == "l")
		{
			yearent_str = util::int_to_string(log[log.size() - 1].year());
			yearent = log[log.size() -1].year();
			dateent_str = util::int_to_string(log[log.size() - 1].date());
			dateent = log[log.size() -1].date();
		}

		if (dateinq == "d")
		{
			do
			{
				date_good = "n";
	 			std::cout << std::endl << "Enter date (101 thru 1231): ";
				do
				{
					std::cin >> entry;
					dateent_str = entry;
					dateent = util::string_to_int(dateent_str);
					for(int i = 1095; i < 1461; i++)
					{
						if (dateent == vec_dates[i])
							date_good = "y";
					}
					if (date_good == "n")
						std::cout << "Invalid input. Try again:   ";
				} while (date_good == "n");
			
				std::cout << std::endl;
				std::cout << "Enter year  [default " << log[log.size() -1].year() <<  "]: ";
				do
				{
					std::cin >> entry;
					yearent_str = entry;
					if (yearent_str == "")
					{
						yearent = log[log.size() -1].year();
						yearent_str = util::int_to_string(log[log.size() -1].year());
					}
					else
						yearent = util::string_to_int(yearent_str);
					if (yearent < 1900 || yearent > 3000)
						std::cout << "Invalid input. Try again:       ";
				} while (yearent < 1900 || yearent > 3000); 
				std::cout << std::endl;
				if (dateent == 229 && yearent % 4 != 0)
				{
					std::cout << yearent << " not a leap year. Try again." << std::endl;
					date_good = "n";
				}
				if (date_good == "y")
				{
					future = "n";
					if (time_fun == "y" && (yearent > year_today || (yearent == year_today && dateent > date_today)))
					{
						std::cout << "Future date input. Try again." << std::endl;
						future = "y";
					}
					proceed = "y";
					precede_entry = "n";
					if ((yearent < log[log.size() -1].year()) ||(yearent == year_today && dateent < log[log.size() -1].date()))
					{
						precede_entry = "y";
						std::cout << "Warning: Entry predates last logged entry (" << log[log.size() -1].dateStr() << ")" << std::endl;
						std::cout << "Do you wish to continue? [y]: ";
						do
						{
							std::cin >> entry;
							proceed = entry;
							if (proceed == "")
								proceed = "y";
							proceed = util::all_lower(proceed);
							if (proceed == "n")
								precede_entry = "n";
							if (proceed != "y" && proceed != "n")
								std::cout << "            Enter 'y' or 'n': ";
						} while(proceed != "y" && proceed != "n");
					}
				}
			} while(future == "y" || proceed == "n" || date_good == "n");  //  || precede_entry == "y"
			std::cout << std::endl;
		}		// if date selection = d
	
	callnumselect = "bad";
	std::cout << "AIRCRAFT SELECTION" << std::endl;

	do
	{
		std::cout << "Enter 3-6 char aircraft call number" << std::endl;
		std::cout << util::string_print_spacer(planes[log[log.size() - 1].index()].callNumber(), 11)
				<< "Last logged aircraft [" << planes[log[log.size() - 1].index()].callNumber() << "]: ";
	do
	{
		std::cin >> entry;
		callnument = entry;
		if (callnument == "")
			{
			callnument = planes[log[log.size() - 1].index()].callNumber();
			plane_ent_index = log[log.size() - 1].index();
			callnumselect = "good";
			}
		if ((callnument.length() < 3 || callnument.length() > 6) && callnument != "")
			std::cout << "Invalid input. Try again: ";
	}while((callnument.length() < 3 || callnument.length() > 6) && callnument != "");

	if (callnumselect == "bad")
	{
		callnument = util::all_caps(callnument);
		callnument = util::remove_leading_blanks(callnument);
		for (unsigned int i = 0; i < planes.size(); i++)
		{
			if (callnument == planes[i].callNumber())
			{
				plane_ent_index = i;
				callnumselect = "good";
				std::cout << std::endl;
			}
		}
	}
	if (callnumselect == "bad")
	{
		std::cout << callnument << " is not in the data base." << std::endl;
		std::cout << "Enter 'r' to reenter or 'a' to add a new plane: ";
		do
		{
			std::cin >> entry;
			new_reent = entry;
			new_reent = util::all_lower(new_reent);
			if (new_reent != "r" && new_reent != "a")
				std::cout << "                             Enter 'r' or 'a' : ";
		} while(new_reent != "r" && new_reent != "a");
		std::cout << std::endl;
		if (new_reent == "a")
		{
			do
			{
				std::cout << std::endl;
				std::cout << "Enter aircraft model: ";
				std::cin >> entry;
				new_model = entry;
				new_model = util::all_caps(new_model);
				new_model = util::remove_leading_blanks(new_model);
				new_model = util::replace_comma(new_model);
				model_found = "no";
				pl = 0;
				do
				{
					if (new_model == planes[pl].model())
					{
						model_found = "yes";
						new_type = planes[pl].type();
						new_rg = planes[pl].rg();
						new_hp = planes[pl].hp();
						new_tw = planes[pl].tw();
					}
					pl += 1;
				} while(model_found == "no" && pl < planes.size());
				if (model_found == "no")
				{
					std::cout << std::endl;
					std::cout << "Enter aircraft category or class (ie SEL MEL ROTO): ";
					std::cin >> entry;
					new_type = entry;
					new_type = util::all_caps(new_type);
					new_type = util::remove_leading_blanks(new_type);
					new_type = util::replace_comma(new_type);
					std::cout << std::endl;
					std::cout << "Retractable gear?     ";
					do
					{
						std::cin >> entry;
						new_rg = entry;
						new_rg = util::all_lower(new_rg);
						if (new_rg != "y" && new_rg != "n")
							std::cout << "Enter y or n:         ";
					} while(new_rg != "y" && new_rg != "n");
					if (new_rg == "y") 
						new_rg = "RG";
					else
						new_rg = "NRG";
					std::cout << std::endl;
					std::cout << "Greater than 200hp?   ";
					do
					{
						std::cin >> entry;
						new_hp = entry;
						new_hp = util::all_lower(new_hp);
						if (new_hp != "y" && new_hp != "n")
						std::cout << "Enter y or n:         ";
					} while(new_hp != "y" && new_hp!= "n");
					if (new_hp == "y") 
						new_hp = "HP";
					else
						new_hp = "NHP";
					std::cout << std::endl;
					std::cout << "Tail wheel?           ";
					do
					{
						std::cin >> entry;
						new_tw = entry;
						new_tw = util::all_lower(new_tw);
						if (new_tw != "y" && new_tw != "n")
							std::cout << "Enter y or n:         ";
					} while(new_tw != "y" && new_tw != "n");
					if (new_tw == "y") 
						new_tw = "TW";
					else
						new_tw = "NTW";
					}
				std::cout << std::endl;
				std::cout << "Call Number:      ";
				std::cout << callnument << std::endl;
				std::cout << "Model             ";
				std::cout << new_model << std::endl;
				std::cout << "Category/Class    ";
				std::cout << new_type  << std::endl;
				std::cout << "Retractable gear? ";
				if (new_rg == "NRG")
					std::cout << "no" << std::endl;
				else
					std::cout << "yes" << std::endl;
				std::cout << "High performance? ";
				if (new_hp == "NHP")
					std::cout << "no" << std::endl;
				else
					std::cout << "yes" << std::endl;
				std::cout << "Tail wheel?       ";
				if (new_tw == "NTW")
					std::cout << "no" << std::endl;
				else
					std::cout << "yes" << std::endl;
				std::cout << std::endl;
				std::cout << "Are you ready to enter the new plane in the database? ";
				do
				{
					std::cin >> entry;
					new_plane_inq = entry;
					new_plane_inq = util::all_lower(new_plane_inq);
					if (new_plane_inq != "y" && new_plane_inq != "n")
				std::cout << "Enter 'y' or 'n': "; 
					}while (new_plane_inq != "y" && new_plane_inq != "n");
				std::cout << std::endl;
				if (new_plane_inq == "n")
					{
					std::cout << "Reenter aircraft call sign: ";
					std::cin >> entry;
					callnument = entry;
					callnument = util::all_caps(callnument);
					callnument = util::remove_leading_blanks(callnument);
					callnument = util::replace_comma(callnument);
					std::cout << std::endl;
					}
				}while (new_plane_inq == "n");
						
			callnumselect = "good";
			order_found = "no";
			plane_ent_index = planes.size();
			std::string plane_line = callnument + "," + new_model + "," + new_type + "," + new_rg + "," + new_hp + "," + new_tw;
			//planes_tmp.setPlane(plane_line, new_type, new_model, callnument, new_rg, new_hp, new_tw);
			planes.push_back(Plane(plane_line, new_type, new_model, callnument, new_rg, new_hp, new_tw));
			}    // if  reent a     
		}        // if  calnumselect = "bad"
	}while (callnumselect == "bad");
std::cout << std::endl;

int used;
deslast_index = log[log.size() - 1].index();
deslast_airport_id = airports[deslast_index].id();
deslast_airport = airports[deslast_index].name();
deslast_airport_suf = airports[deslast_index].suf();
deslast_str = deslast_airport_id + "  " + deslast_airport + ", " + deslast_airport_suf;

for(int d = 1; d < 3; ++d)
	{
	if (d == 1)
		std::cout << "DEPARTURE AIRPORT:" << std::endl;
	else if (d == 2)
		std::cout << "DESTINATION AIRPORT:" << std::endl;
	do
	{
		do
		{
			std::cout << "Enter airport ID or first 3 to 5 characters of airport name:" << std::endl;
			if (d == 1)
				std::cout << util::string_print_spacer(deslast_str, 37) << "Last logged airport [" << deslast_str << "]: ";
			if (d == 2)
				std::cout << util::string_print_spacer(dep_str, 44) << "Local flight [" << dep_str << "]: ";
			depselect = "bad";
			std::cin.getline(entry, 50, '\n');
			airport_select = entry;
			if (airport_select == "" && d == 1)
				{
				depent = deslast_airport_id;
				dep_ind_new = deslast_ind;
				dep_id_new = deslast_airport_id;
				dep_new = deslast_airport;
				dep_suf_new = deslast_airport_suf;
				depselect = "good";
				}
			if (airport_select == "" && d == 2)
				{
				desent = depent;
				des_ind_new = dep_ind_new;
				des_id_new = dep_id_new;
				des_new = dep_new;
				des_suf_new = dep_suf_new;
				depselect = "good";
				}
			airport_select = util::remove_leading_blanks(airport_select);
			airport_select = util::replace_comma(airport_select);
			airport_select = util::all_caps(airport_select);
			std::cout << std::endl;
			if ((airport_select.length() < 3 || airport_select.length() > 5) && airport_select != "")
				std::cout << "Invalid input. Enter 3 to 5 characters or hit 'Enter': ";
			} while ((airport_select.length() < 3 || airport_select.length() > 5) && airport_select != "");
				
			if (depselect == "bad")
			{	
				airport_list_select = -1;
				airport_candidates_num = -1;
				used = -1;
				if (airport_select.length() < 5)
					{
					for (unsigned int i = 0; i < airports.size(); i++)
						{
						if (airport_select == util::all_caps(airports[i].id()))
							{
							airport_candidates_num += 1;
							airport_candidates[airport_candidates_num] = i;
							used = i;
							}
						}
					}
		
				for (unsigned int i = 0; i < airports.size(); i++)
					{
					if (airport_select == util::all_caps(airports[i].name().substr(0, airport_select.length())) && used != i)
						{
						airport_candidates_num += 1;
						airport_candidates[airport_candidates_num] = i;
						}
					}
				if (airport_candidates_num > 0)
					{
					for (int can_index = 0; can_index <= airport_candidates_num; can_index ++)
						std::cout << setw(2) << can_index + 1 << "  " << airports[airport_candidates[can_index]].id()
															<< util::string_print_spacer(airports[airport_candidates[can_index]].id(), 4) 
															<< "  " << airports[airport_candidates[can_index]].name()
															<< ", " << airports[airport_candidates[can_index]].suf() << std::endl;
					}
		
				if (airport_candidates_num == -1)
					std::cout << airport_select << " is not in the database." << std::endl;

				if (airport_candidates_num == 0 && d == 1)
				{
					depent = airports[airport_candidates[0]].id();
					dep_ind_new = airport_candidates[0];
					dep_id_new = airports[airport_candidates[0]].id();
					dep_new = airports[airport_candidates[0]].name();
					dep_suf_new = airports[airport_candidates[0]].suf();
					depselect = "good";
				}
				if (airport_candidates_num == 0 && d == 2)
				{
					desent = airports[airport_candidates[0]].id();
					des_ind_new = airport_candidates[0];
					des_id_new = airports[airport_candidates[0]].id();
					des_new = airports[airport_candidates[0]].name();
					des_suf_new = airports[airport_candidates[0]].suf();
					depselect = "good";
				}
				if (airport_candidates_num > 0 || airport_candidates_num == -1)
				{
					std::cout << "'r' Reenter 3 to 5 character search" << std::endl;				
					if (airport_select.length() < 5 && used == -1)					
						std::cout << "'a' Add airport with id '" << airport_select << "'" << std::endl;

					std::cout << "'n' Add airport with new id         " << std::endl;
					std::cout << std::endl;
					if (airport_candidates_num > 0) 
						std::cout << "Enter number to select airport (1 to " << setw(2) << airport_candidates_num + 1 << ") " << "or" << std::endl;
					if (airport_select.length() < 5 && used == -1)
						std::cout << "Enter option 'r' 'a' or 'n': ";
					if (airport_select.length() == 5 || (airport_select.length() <5 && used > -1))
						std::cout << "Enter option 'r' or 'n': ";
					do
					{
						std::cin.getline(entry, 50, '\n');
						airport_list_select_str = entry;
						airport_list_select_str = util::all_lower(airport_list_select_str);
						airport_list_select = util::string_to_int(airport_list_select_str);
						if ((airport_select.length() == 5 || (airport_select.length() <5 && used > -1)) && airport_list_select_str == "a")
							airport_list_select_str = "n";
						if ((airport_list_select < 1 || airport_list_select > airport_candidates_num + 1 ) && airport_list_select_str != "r" &&
							airport_list_select_str != "a" && airport_list_select_str != "n")
						{
							std::cout << "Enter ";
							if (airport_candidates_num > 0)
							std::cout << "(1 to " << airport_candidates_num + 1 << "), ";
							if (airport_select.length() < 5 && used == -1)
								std::cout << "'r', 'a' or 'n': ";
							if (airport_select.length() == 5 || (airport_select.length() < 5 && used > -1))
								std::cout << "'r' or 'n': ";
						}
						}while((airport_list_select < 1 || airport_list_select > airport_candidates_num + 1 ) && airport_list_select_str != "r" &&
							    airport_list_select_str != "a" && airport_list_select_str != "n");  
	
					if (airport_list_select > 0 && d == 1)
						{
						dep_ind_new = airport_candidates[airport_list_select - 1];
						dep_id_new = airports[airport_candidates[airport_list_select - 1]].id();
						depent = dep_id_new;
						dep_new = airports[airport_candidates[airport_list_select - 1]].name();
						dep_suf_new = airports[airport_candidates[airport_list_select - 1]].suf();
						depselect = "good";
						std::cout << std::endl;
						}
					if (airport_list_select > 0 && d == 2)
						{
						des_ind_new = airport_candidates[airport_list_select - 1];
						des_id_new = airports[airport_candidates[airport_list_select - 1]].id();
						desent = des_id_new;
						des_new = airports[airport_candidates[airport_list_select - 1]].name();
						des_suf_new = airports[airport_candidates[airport_list_select - 1]].suf();
						depselect = "good";
						std::cout << std::endl;
						}	
					}
				}
				if (depselect == "good")
					{
					if (d == 1)
						{
						std::cout << "Selected airport:  " << dep_id_new << "  " << dep_new << " " << dep_suf_new << std::endl;
						dep_str = dep_id_new + "  " + dep_new + ", " + dep_suf_new;
						}
					if (d == 2)
						std::cout << "Selected airport:  " << des_id_new << "  " << des_new << " " << des_suf_new << std::endl;
					std::cout << std::endl;
					airport_candidates_num = -1;
					airport_list_select_str = "z";
					airport_list_select = -1;
					}

			}while(airport_list_select_str == "r");
	if (airport_list_select_str == "n" || airport_list_select_str == "a")
		{
		id_new = airport_select;
		if (airport_list_select_str == "n")
			{
			std::cout << std::endl << "Enter airport identifier (3 or 4 characters): ";
				do
					{
					airport_id_used = "n";
					std::cin.getline(entry, 50, '\n');
					id_new = entry;
					id_new = util::remove_leading_blanks(id_new);
					id_new = util::replace_comma(id_new);
					id_new = util::all_caps(id_new);
					for (unsigned int i = 0; i < airports.size(); i++)
						{
						if (id_new == airports[i].id())
						{
							std::cout << id_new << "  " << airports[i].name() << ", " << airports[i].suf() << " already exists." << std::endl;
							std::cout << "Try another airport id:                   ";
							airport_id_used = "y";
						}
						}
					if (id_new.length() < 3 || id_new.length() > 4)
						std::cout << "Incorrect number of characters. Try again :   "; 
					}while(id_new.length() < 3 || id_new.length() > 4 || airport_id_used == "y");
			}
		if (d == 1)
			depent = id_new;
		if (d == 2)
			desent = id_new;
		do
		{
			std::cout << std::endl << "Enter airport name up to 26 charcaters: ";
			do
				{
				std::cin.getline(entry, 50, '\n');
				name_new = entry;
				name_new = util::remove_leading_blanks(name_new);
				name_new = util::replace_comma(name_new);
				std::cout << std::endl;
				if (name_new.length() > 26)
					{
					std::cout << "Airport name longer than 26 characters. " << std::endl;
					std::cout << "Reenter airport name:                   ";
					}
				}while(name_new.length() > 26);
			if (d == 1)
				dep_new = name_new;
			if (d == 2)
				des_new = name_new;
			std::cout << "Enter a two or three character" << std::endl;
			std::cout << "state, province or country code:      ";
			do
				{
				cin.getline(entry, 50, '\n');
				suf_new = entry;
				suf_new = util::remove_leading_blanks(suf_new);
				suf_new = util::replace_comma(suf_new);
				suf_new = util::all_caps(suf_new);
				std::cout << std::endl;
				if (suf_new.length() < 2 || suf_new.length() > 3)
					std::cout << "Incorrect code length. Reenter:       ";
				}while(suf_new.length() < 2 || suf_new.length() > 3);
			if (d == 1)
				dep_suf_new = suf_new;
			if (d == 2)
				des_suf_new = suf_new;
	
			std::cout << "Enter the latitude degrees (-90 to 90)      ";	
			do
			{
				std::cin >> entry;
				dep_new_lat_deg_str = entry;
				dep_new_lat_deg = util::string_to_double(dep_new_lat_deg_str);
				std::cout << std::endl;
				if (dep_new_lat_deg < -90 || dep_new_lat_deg > 90 || (dep_new_lat_deg == 0 && dep_new_lat_deg_str != "0"))
					std::cout << "Response outside range. Reenter             ";
			}while (dep_new_lat_deg < -90 || dep_new_lat_deg > 90 || (dep_new_lat_deg == 0 && dep_new_lat_deg_str != "0"));

			std::cout << "Enter the latitude minutes (-59.9 to 59.9)  ";					
			do
			{
				std::cin >> entry;
				dep_new_lat_min_str = entry;
				dep_new_lat_min = util::string_to_double(dep_new_lat_min_str);
				std::cout << std::endl;
				if (dep_new_lat_min < -59.9 || dep_new_lat_min > 59.9 || (dep_new_lat_min == 0 && dep_new_lat_min_str != "0"))
					std::cout << "Response outside range. Reenter             ";
			} while (dep_new_lat_min < -59.9 || dep_new_lat_min > 59.9 || (dep_new_lat_min == 0 && dep_new_lat_min_str != "0"));

			std::cout << "Enter the longitude degrees (-180 to 180)   ";
			do
			{
				std::cin >> entry;
				dep_new_lon_deg_str = entry;
				dep_new_lon_deg = util::string_to_double(dep_new_lon_deg_str);
				std::cout << std::endl;
				if (dep_new_lon_deg < -180 || dep_new_lon_deg > 180 || (dep_new_lon_deg == 0 && dep_new_lon_deg_str != "0"))
					std::cout << "Response outside range. Reenter             ";
			} while (dep_new_lon_deg < -180 || dep_new_lon_deg > 180 || (dep_new_lon_deg == 0 && dep_new_lon_deg_str != "0"));

			std::cout << "Enter the longitude minutes (-59.9 to 59.9) ";					
			do
				{
				cin.getline(entry, 50, '\n');
				dep_new_lon_min_str = entry;
				dep_new_lon_min = util::string_to_double(dep_new_lon_min_str);
				std::cout << std::endl;
				if (dep_new_lon_min < -59.9 || dep_new_lon_min > 59.9 || (dep_new_lon_min == 0 && dep_new_lon_min_str != "0"))
					std::cout << "Response outside range. Reenter             ";
				}while (dep_new_lon_min < -59.9 || dep_new_lon_min > 59.9 || (dep_new_lon_min == 0 && dep_new_lon_min_str != "0"));
			std::cout << std::endl;
			std::cout << "Airport id  " << id_new << std::endl;
			std::cout << "Location    " << name_new << ", " << suf_new << std::endl;
			std::cout << "Lat deg min " << dep_new_lat_deg << " " << dep_new_lat_min << std::endl;
			std::cout << "Lon deg min " << dep_new_lon_deg << " " << dep_new_lon_min << std::endl << std::endl;
			std::cout << "Are you ready to enter the new airport in the database? ";
			do
			{
				std::cin >> entry;
				new_airport_inq = entry;
				new_airport_inq = util::all_lower(new_airport_inq);
				if (new_airport_inq != "y" && new_airport_inq != "n")
					std::cout << "                                     Enter  'y' or 'n': ";
			} while (new_airport_inq != "y" && new_airport_inq != "n");
			std::cout << std::endl;
			if (new_airport_inq == "n")
			{
				do
				{
					std::cout << "Renter airport identifier (3 or 4 characters): ";
					airport_id_used = "n";
					std::cin >> entry;
					id_new = entry;
					id_new = util::remove_leading_blanks(id_new);
					id_new = util::replace_comma(id_new);
					id_new = util::all_caps(id_new);
					std::cout << std::endl;
					for (unsigned int i = 0; i < airports.size(); ++i)
						{
						if (id_new == airports[i].id())
							{
							std::cout << id_new << "  " << airports[i].name() << ", " << airports[i].suf() << " already exists." << std::endl;
							std::cout << "Try another airport id:                   ";
							airport_id_used = "y";
							}
						}
					if (id_new.length() < 3 || id_new.length() > 4)
						std::cout << "Incorrect airport id length. Reenter:          ";
					}while(id_new.length() < 3 || id_new.length() > 4 || airport_id_used == "y");
				}
			} while (new_airport_inq == "n");

			airport_line = id_new + "," + name_new + "," + suf_new + "," + dep_new_lat_deg_str + "," + dep_new_lat_min_str + "," + 
									dep_new_lon_deg_str + "," + dep_new_lon_min_str;

			airport_tmp.setAirport(airport_line, id_new, name_new, suf_new, dep_new_lat_deg + dep_new_lat_min / 60,
																			dep_new_lon_deg + dep_new_lon_min / 60);
			airports.push_back(airport_tmp);

			if (d == 1)
			{
				dep_str = depent + "  " + dep_new + ", " + dep_suf_new;
				dep_ind_new = airports.size() - 1;
			}
			if (d == 2)
				des_ind_new = airports.size() - 1;
		}             //  airport_list_select_str = a or n 
	}				// for d=1 to 2
landingent = 0;
night_landingent = 0;
studentnum = 0;
dualent = 0;
sicent = 0;
sftent = 0;
landingent_str = "0";
night_landingent_str = "0";
studentnum_str = "0";
std::string stu_num_str;
dualent_str = "0";
sicent_str = "0";
sftent_str = "0";
std::cout << "All times are entered X10. Example: 2.7 is entered as 27" << std::endl;
std::cout << "Default entries assume PIC VFR day; no CFI or dual" << std::endl;
std::cout << "Defults adjust as other entries are made" << std::endl << std::endl;
std::cout << "Enter total hours      (greater than 0): ";
do
{
	std::cin >> entry;
	timeent_str = entry;
	timeent = util::string_to_int(timeent_str);
	if (timeent < 1)
		std::cout << "Invalid input. Try again: "; 
} while(timeent < 1);
std::cout << std::endl;
std::cout << "Enter night    (0 to " << setw(2) << timeent << ")  [default = 0]: ";

do
{
	std::cin >> entry;
	nightent_str = entry;
	if (nightent_str == "")
		nightent_str = "0";
	nightent = util::string_to_int(nightent_str);
	if (nightent < 0 || nightent > timeent || (nightent == 0 && nightent_str != "0"))
		std::cout << "               Invalid input. Try again: "; 
	}while(nightent < 0 || nightent > timeent || (nightent == 0 && nightent_str != "0"));
std::cout << std::endl;

if (timeent > nightent)
{
	if (nightent > 0)
		std::cout << "Enter # day landings [default = 0]: ";
	if (nightent == 0)
		std::cout << "Enter # day landings [default = 1]: ";
	do
	{
		std::cin >> entry;
		landingent_str = entry;
		if (landingent_str == "" && nightent > 0)
			landingent_str = "0";
		if (landingent_str == "" && nightent == 0)
			landingent_str = "1";
		landingent = util::string_to_int(landingent_str);
		if (landingent < 0 || (landingent == 0 && landingent_str != "0"))
			std::cout << "               Invalid input. Try again: ";
	} while(landingent < 0 || (landingent == 0 && landingent_str != "0"));
	std::cout << std::endl;
}

if (nightent > 0)
{
	std::cout << "Enter # night landings    [default = 1]: ";
	do
	{
		std::cin >> entry;
		night_landingent_str = entry;
		if (night_landingent_str == "")
			night_landingent_str = "1";
		night_landingent = util::string_to_int(night_landingent_str);
		if (night_landingent < 0 || (night_landingent == 0 && night_landingent_str != "0"))
			std::cout << "               Invalid input. Try again: ";
	} while(night_landingent < 0 || (night_landingent == 0 && night_landingent_str != "0"));
	std::cout << std::endl;
}

std::cout << "Enter # approaches [default = 0]: ";
do
{
	std::cin >> entry;
	apprent_str = entry;
	if (apprent_str == "")
		apprent_str = "0";
	apprent = util::string_to_int(apprent_str);
	if (apprent < 0 || (apprent == 0 && apprent_str != "0"))
		std::cout << "               Invalid input. Try again: ";
} while(apprent < 0 || (apprent == 0 && apprent_str != "0"));	 		
std::cout << std::endl;

std::cout << "Enter IMC      (0 to " << setw(2) << timeent << ")  [default = 0]: ";	
do
{
	std::cin >> entry;
	ifrent_str = entry;
	if (ifrent_str == "")
		ifrent_str = "0";
	ifrent = util::string_to_int(ifrent_str);
	if (ifrent < 0 || ifrent > timeent || (ifrent == 0 && ifrent_str != "0"))
		std::cout << "               Invalid input. Try again: "; 
} while(ifrent < 0 || ifrent > timeent || (ifrent == 0 && ifrent_str != "0"));
std::cout << std::endl;

std::cout << "Enter sim. IMC (0 to " << setw(2) << timeent - ifrent << ")  [default = 0]: ";
do
{
	std::cin >> entry;
	gadent_str = entry;
	if (gadent_str == "")
		gadent_str = "0";
	gadent = util::string_to_int(gadent_str);
	if (gadent < 0 || gadent > timeent - ifrent || (gadent == 0 && gadent_str != "0"))
		std::cout << "               Invalid input. Try again: ";
} while(gadent < 0 || gadent > timeent - ifrent || (gadent == 0 && gadent_str != "0"));
std::cout << std::endl;

std::cout << "Enter CFI given(0 to " << setw(2) << timeent << ")  [default = 0]: ";		
do
{
	std::cin >> entry;
	cfient_str = entry;
	if (cfient_str == "")
		cfient_str = "0";
	cfient = util::string_to_int(cfient_str);
	if (cfient < 0 || cfient > timeent || (cfient == 0 && cfient_str != "0"))
		std::cout << "               Invalid input. Try again: ";			
} while(cfient < 0 || cfient > timeent || (cfient == 0 && cfient_str != "0"));

if (cfient > 0)
	{
	std::cout << std::endl << "Enter student # (1 to " << students.size() << ")" << std::endl;
	std::cout << "    or " << students.size() + 1 << " for new student: ";			
	do
	{
		std::cin >> entry;
		studentnum_str = entry;
		studentnum = util::string_to_int(studentnum_str);
		if (studentnum < 1 || studentnum > students.size() + 1)
			std::cout << "Invalid input. Try again:  ";
	} while(studentnum < 1 || studentnum > students.size() + 1);

	if (studentnum < students.size() + 1)
		std::cout << "                              " << students[studentnum - 1].firstName() << " " << students[studentnum - 1].lastName() << std::endl;
	if (studentnum == students.size() + 1)
		{
		std::cout << "Enter first name:          ";
		std::cin >> entry;
		student_first_ent = entry;
		student_first_ent = util::remove_leading_blanks(student_first_ent);
		student_first_ent = util::replace_comma(student_first_ent);
		student_first_ent = init_cap(student_first_ent);
		std::cout << std::endl;
		std::cout << "Enter last name:           ";
		cin.getline(entry, 50, '\n');
		student_last_ent = entry;
		student_last_ent = util::remove_leading_blanks(student_last_ent);
		student_last_ent = util::replace_comma(student_last_ent);
		student_last_ent = init_cap(student_last_ent);
			
		stu_num_str = int_to_string(students.size() + 1);
		student_line =stu_num_str + "," + student_last_ent + "," + student_first_ent;

		student_tmp.setStudent(student_line, student_last_ent, student_first_ent);
		students.push_back(student_tmp);
		}
	}
	std::cout << std::endl;
if (timeent - cfient > 0)
	{
	std::cout << "Enter dual     (0 to " << setw(2) << timeent - cfient << ")  [default = 0]: ";			
	do
	{
		std::cin >> entry;
		dualent_str = entry;
		if (dualent_str == "")
			dualent_str = "0";
		dualent = util::string_to_int(dualent_str);
		if (dualent < 0 || dualent > timeent - cfient || (dualent == 0 && dualent_str != "0"))
			std::cout << "               Invalid input. Try again: ";
	} while(dualent < 0 || dualent > timeent - cfient || (dualent == 0 && dualent_str != "0"));
	std::cout << std::endl;
	}
std::cout << "Enter PIC      (0 to " << setw(2) << timeent << ") [default = " << setw(2) << timeent << "]: ";		
do
{
	std::cin >> entry;
	picent_str = entry;
	if (picent_str == "")
		picent_str = timeent_str;
	picent = util::string_to_int(picent_str);
	if (picent < 0 || picent > timeent || (picent == 0 && picent_str != "0"))
		std::cout << "               Invalid input. Try again: ";
} while(picent < 0 || picent > timeent || (picent == 0 && picent_str != "0"));
std::cout << std::endl;

if (picent < timeent)	
{
	std::cout << "Enter SIC      (0 to " << setw(2) << timeent - picent << ")  [default = 0]: ";
	do
	{
		std::cin >> entry;
		sicent_str = entry;
		if (sicent_str == "")
			sicent_str = "0";
		sicent = util::string_to_int(sicent_str);
		if (sicent < 0 || sicent > timeent - picent || (sicent == 0 && sicent_str != "0"))
			std::cout << "               Invalid input. Try again: ";
	} while(sicent < 0 || sicent > timeent - picent || (sicent == 0 && sicent_str != "0"));
	std::cout << std::endl;
}

if (timeent - (picent + sicent) > 0)
{
	std::cout << "Enter Safety   (0 to " << setw(2) << timeent - (picent + sicent) << ")  [default = 0]: ";
	do
	{		
		std::cin >> entry;
		sftent_str = entry;
		if (sftent_str == "")
			sftent_str = "0";
		sftent = util::string_to_int(sftent_str);
		if (sftent < 0 || sftent > timeent - (picent + sicent) || (sftent == 0 && sftent_str != "0"))
			std::cout << "               Invalid input. Try again: ";
		}while(sftent < 0 || sftent > timeent - (picent + sicent) || (sftent == 0 && sftent_str != "0"));
	std::cout << std::endl;
	}
std::cout << "Enter comments: ";
std::cin >> entry;
commentent = entry;
commentent = util::remove_leading_blanks(commentent);
commentent = util::replace_comma(commentent);
std::cout << std::endl;

std::cout << "Date  " << util::int_to_string_date_format(yearent, dateent) << std::endl;
std::cout << "Plane " << callnument << std::endl;
std::cout << "Dep   " << depent << "  " << dep_new << " " << dep_suf_new << std::endl;
std::cout << "Des   " << desent << "  " << des_new << " " << des_suf_new << std::endl << std::endl;

std::cout << "Is this line of data OK to enter in the logbook? [y]: ";
do
{
	std::cin >> entry;
	verify_log = entry;
	if (verify_log == "")
		verify_log = "y";
	verify_log = util::all_lower(verify_log);
	if (verify_log != "n" && verify_log != "y")
		std::cout << "                                   Enter 'y' or 'n': ";
} while (verify_log != "n" && verify_log != "y");
std::cout << std::endl;
    
if (verify_log == "y")
{
	log_line = (yearent_str + "," + dateent_str + "," + callnument + "," + depent + "," + desent + "," + landingent_str + "," +
					night_landingent_str + "," + apprent_str + "," + timeent_str + "," + nightent_str + "," + ifrent_str + "," +
					gadent_str + "," + cfient_str + "," + studentnum_str + "," + dualent_str + "," + picent_str + "," +
					sicent_str + "," + sftent_str + "," + commentent);

	log_tmp.set_logLine (log_line, yearent, dateent, date_format(yearent_str, dateent_str), plane_ent_index, dep_ind_new, des_ind_new,
						landingent, night_landingent, apprent, timeent, nightent, ifrent, gadent, cfient, studentnum, dualent, picent,
						sicent, sftent);
	if (precede_entry == "y")
	{
		log_counter = log.size();
		do
		{
			log_counter -= 1;
			log[log_counter].year();
			log[log_counter].date();
		} while(yearent < log[log_counter].year() || 
					(yearent == log[log_counter].year() && dateent < log[log_counter].date()));
		log.push_back(log[log.size() - 1]);				
		for (int i = log.size() - 3; i > log_counter; --i)
			log[i+1] = log[i];

		log[log_counter + 1] = log_tmp;
	}
	else
		log.push_back(log_tmp);
}

std::cout << "Do you have another log entry? [y]: ";

do
{ 
	cin.getline(entry, 50, '\n');
	newentry = entry;
	if (newentry == "")
		newentry = "y";
	newentry = util::all_lower(newentry);
	if (newentry != "n" && newentry != "y")
		std::cout << "Enter 'y' or 'n': ";
}while (newentry != "n" && newentry != "y");
std::cout << std::endl;

} while (newentry == "y");

// update all the input_* files

ofstream logbook_writer;
logbook_writer.open("logbook.csv");
logbook_writer << file_header_1 << std::endl;
logbook_writer << file_header_2 << std::endl;
for (unsigned int i = 0; i < log.size(); i++)
	logbook_writer << log[i].logLine() << std::endl;
logbook_writer << file_header_2 << std::endl;
logbook_writer << file_header_1 << std::endl;
logbook_writer.close();

ofstream new_airport_output;
new_airport_output.open("input_airports.txt");
for (unsigned int i = 0; i < airports.size(); i++)
	new_airport_output << airports[i].logLine() << std::endl;
new_airport_output.close();

ofstream new_plane_output;
new_plane_output.open("input_planes.txt");
for (unsigned int i = 1; i < planes.size(); i++)
	new_plane_output << planes[i].logLine() << std::endl;
new_plane_output.close();

ofstream new_student_output;
new_student_output.open("input_students.txt");
for (unsigned int i = 0; i < students.size(); i++)
	new_student_output << students[i].logLine() << std::endl;
new_student_output.close();
#endif
}               // bracket for if select section from 648